module.exports = app => {
  class RoomController extends app.Controller {
    async create() {
      console.log(this.ctx.state.user)
      let serialNumber = 1;
      let lastRoom = await this.ctx.model.Room.find({}).sort('-insert_time').limit(1)
      if (lastRoom && lastRoom.length && lastRoom[0].serialNumber) serialNumber = lastRoom[0].serialNumber + 1
      try {
        let roomInfo = await this.ctx.model.Room.create({
          serialNumber: serialNumber,
          player: [this.ctx.state.user.userInfo['_id']],
          owner: this.ctx.state.user.userInfo['_id'],
          insert_time: new Date(),
          gameInfo: {
            playerInfo: [{ //玩家状态
              name: this.ctx.state.user.userInfo.username,
              id: this.ctx.state.user.userInfo['_id'],
              headImage: this.ctx.state.user.userInfo.headImage,
              score: 1000, //!默认初始1000分
            }]
          }
        })
        this.ctx.socket.emit(this.ctx.state.user.userInfo['_id'], {
          status: 0,
          message: roomInfo
        });
      } catch (e) {
        this.ctx.socket.emit(this.ctx.state.user.userInfo['_id'], {
          message: '创建失败',
          status: -1
        });
      }
    }

  }
}